boolean firstPress = false;
int x0 ;
int y0 ;
int x1 ;
int y1 ;

void setup() {
  size(640, 360);
  background(255);
  noFill();
}

void mousePressed() {
  if (firstPress) {
    x1 = mouseX;
    y1 = mouseY;
    firstPress = false;
    stroke(random(1, 250), random(1, 250), random(1, 250));
  }
  else {
    x0 = mouseX;
    y0 = mouseY;
    firstPress = true;
  }
}

void draw() {
  //if (firstPress && x1!= mouseX && y1 !=  mouseY) {
  if (firstPress) {
    //background(255);
    x1 = mouseX;
    y1 = mouseY;
stroke(random(1, 250), random(1, 250), random(1, 250));
    int r = round(sqrt(((x1-x0)*(x1-x0))+((y1-y0)*(y1-y0))));


    int x, y;
    int endX, endY;
    // f(x,y) = x^2 + y^2 - r = 0

    int h = 1-r;
    x = x0;
    y = y0+r;
    endX = x1;
    while (2*(x-x0)*(x-x0) < r*r) {
      point(x, y);
      point(x,y0-(y-y0));
      point(x0-(x-x0),y);
      point(x0-(x-x0),y0-(y-y0));

      point(x0+(y-y0), y0+(x-x0));
      point(x0+(y0-y),y0+(x-x0));
      point(x0+(y-y0),y0+(x0-x));
      point(x0+(y0-y),y0+(x0-x));

      //print(x); print(','); print(y);print('\n');

      x += 1;
      y += ((h>0)?-1:0);
      h += 2*(x-x0) + 3 + ((h>0)?-2*(y-y0)+2:0);

    }

  }
}


